package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;


    //查询检查组（分页查询）
    @PostMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }
    //新增检查组（多表添加）
    @PostMapping("/add.do")
    public Result add(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        if (checkGroup == null){
            return new Result(false,"参数异常");
        }
        checkGroupService.save(checkGroup);

        Result result = checkGroupService.add(checkGroup.getId(),checkitemIds);
        return result;
    }
    //修改检查组数据回显
    @GetMapping("/findById.do")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.getById(id);
        return new Result(true,"查询检查组成功",checkGroup);
    }
    //检查项数据回显
    @GetMapping("/findCheckItemIdsByCheckGroupId.do")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        Result result = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return result;
    }

    //修改功能
    @PostMapping("/edit.do")
    public Result update(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        if (checkGroup == null){
            return new Result(false,"参数异常");
        }
        //修改多种关联的就先删除后添加
        checkGroupService.deleteCheckitemId(checkGroup.getId());

        checkGroupService.updateById(checkGroup);
        Result result = checkGroupService.add (checkGroup.getId(),checkitemIds);
        return result;
    }
    //套餐检查组查询
    @GetMapping("/findAll.do")
    public Result findAll(){
        Result result = checkGroupService.findAll();
        return result;
    }
    //套餐检查组回显
    @GetMapping("/findCheckGroupIdsBySetmealId.do")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        Result result = checkGroupService.findCheckGroupIdsBySetmealId(id);
        return result;
    }
}
